﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using DevExpress.XtraEditors.DXErrorProvider;
using QLKDS.DTO;
using DevExpress.XtraEditors.Controls;

namespace QLKDS.Forms
{
    public partial class frmBookEdit : XtraForm
    {
        #region Properties
        private BooksManager bookManager=new BooksManager();
        public BookInfo Book { get; set; }
        private CategoryManager categoryManager = new CategoryManager();
        private AuthorManager authorManager = new AuthorManager();
        private PublisherManager publisherManager = new PublisherManager();

        #endregion

        #region Init Form
        public frmBookEdit()
        {
            InitializeComponent();
            this.Book = new BookInfo();

        }
        public void Load_info()
        {

            this.ccbeAuthors.Properties.DataSource = authorManager.ListAuthor();
            this.ccbeAuthors.Properties.DisplayMember = "Name";
            this.ccbeAuthors.Properties.ValueMember = "Id";

            this.ccbeCategory.Properties.DataSource = categoryManager.ListCategory();
            this.ccbeCategory.Properties.DisplayMember = "Name";
            this.ccbeCategory.Properties.ValueMember = "Id";

            this.luePublisher.Properties.DataSource = publisherManager.ListPublisher();
            this.luePublisher.Properties.DisplayMember = "Name";
            this.luePublisher.Properties.ValueMember = "Id";
        }

        private void ccbeAuthors_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            ButtonEdit editor = (ButtonEdit)sender;
            EditorButton Button = e.Button;
            //if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            //{
            //    var form = new frmAuthorsAdd(this);
            //    form.ShowDialog();
            //}
        }

        private void ccbeCategory_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            ButtonEdit editor = (ButtonEdit)sender;
            EditorButton Button = e.Button;
            //if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            //{
            //    var form = new frmCategoryAdd(this);
            //    form.ShowDialog();
            //}
        }

        private void luePublisher_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            ButtonEdit editor = (ButtonEdit)sender;
            EditorButton Button = e.Button;
            //if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            //{
            //    var form = new frmPublisherAdd(this);
            //    form.ShowDialog();
            //}
        }

        private void frmBookEdit_Load(object sender, EventArgs e)
        {
            this.txtCode.Text = this.Book.Code;
            this.txtName.Text = this.Book.Name;
        //    this.ccbeAuthors.Text = this.Book.;
            this.luePublisher.Text = this.Book.Publisher;
        //    this.ccbeCategory.Text = String.Empty;
            this.txtDiscountRetail.Text = this.Book.DiscountRetail.ToString();
            this.txtDiscountWholesale.Text = this.Book.DiscountWholesale.ToString();
            this.txtPrice.Text = this.Book.Price.ToString();
            this.dteModifiedDate.DateTime = DateTime.Now;
        }
        #endregion
       
        #region Methods

        private void SetMaxLengthOfTextBoxes()
        {
            this.txtName.Properties.MaxLength = 32;
            this.txtCode.Properties.MaxLength = 8;
        }

        /// <summary>
        /// Clear all Textboxes on this form
        /// </summary>
        private void ClearTextBoxes()
        {
            this.txtCode.Text = String.Empty;
            this.txtName.Text = String.Empty;
            this.ccbeAuthors.Text = String.Empty;
            this.luePublisher.Text = String.Empty;
            this.ccbeCategory.Text = String.Empty;
            this.txtDiscountRetail.Text = String.Empty;
            this.txtDiscountWholesale.Text = string.Empty;
            this.txtPrice.Text = string.Empty;
            this.txtDescription.Text = string.Empty;
            this.txtName.Focus();
        }
        #endregion

        #region Validate

        private void txtName_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepName.ClearErrors();
            }
        }
        private void txtName_Leave(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                this.dxepName.SetError(this.txtName, "Tên sách không được để trống.", ErrorType.User1);
            }
        }

        private void txtCode_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                if (this.bookManager.HasCodeDuplicated(this.txtCode.Text.Trim(),Book.Id))
                {
                    this.dxepCode.SetError(this.txtCode, "Mã sách " + this.txtCode.Text.Trim() + " đã tồn tại trong hệ thống. Hãy chọn một mã sách khác.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtCode.Focus();
                    return;
                }

                this.dxepCode.ClearErrors();
            }
        }
        private void txtCode_Leave(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
                this.dxepCode.SetError(this.txtCode, "Mã sách không được để trống.", ErrorType.User1);
            }
        }





        private void txtCreatedBy_Leave(object sender, EventArgs e)
        {
            if (this.txtModifiedBy.Text.Trim() == String.Empty)
            {
                this.dxepCreatedBy.SetError(this.txtCode, "Lựa chọn tên nhân viên thêm mới.", ErrorType.User1);
            }

        }

        private void txtCreatedBy_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtModifiedBy.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepCreatedBy.ClearErrors();
            }
        }

        #endregion

        #region button_click

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //id = this.Customer.Id;
            var book = new Book();

            //validation name
            if (this.txtName.Text.Trim().Length == 0)
            {
                this.dxepName.SetError(this.txtName, "Tên sách không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            else
            {
                this.dxepName.ClearErrors();
            }
            //end validation name


            //validation Code
            if (this.txtCode.Text.Trim().Length == 0)
            {
                this.dxepCode.SetError(this.txtCode, "Mã sách không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtCode.Focus();
                return;
            }
            else if (this.bookManager.HasCodeDuplicated(this.txtCode.Text.Trim(),Book.Id) == true)
            {
                this.dxepName.SetError(this.txtCode, "Mã sách " + this.txtCode.Text.Trim() + " đã tồn tại trong hệ thống. Hãy chọn một mã sách khác.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            else
            {
                this.dxepCode.ClearErrors();
            }
            //end validation Code

            // End of validation
            ///*****************************************************************/
            //// Save into database
            book.Id = this.Book.Id;
            book.Code = this.txtCode.Text.Trim();
            book.Name = this.txtName.Text.Trim();
            book.Description = this.txtDescription.Text.Trim();
            book.Status = 1;
            book.Price = double.Parse(this.txtPrice.Text.Trim());
            book.DiscountWholesale = double.Parse(this.txtDiscountWholesale.Text.Trim());
            book.DiscountRetail = double.Parse(this.txtDiscountRetail.Text.Trim());
            book.Publisher = this.luePublisher.Text.Trim();
            book.CreatedBy = this.Book.CreatedBy;
            book.CreatedDate = this.Book.CreatedDate;
            book.ModifiedBy = this.txtModifiedBy.Text.Trim();
            book.ModifiedDate = this.dteModifiedDate.DateTime;

            this.bookManager.UpdateBook(book);
            XtraMessageBox.Show("Sửa dữ liệu thành công");
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        #endregion




    }
}
